home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-08-10 | 3.1 KB | 126 lines | [TEXT/CWIE] |
- // ===========================================================================
- // CTCPServerThread.cp ©1995-1998 Metrowerks Inc. All rights reserved.
- // ===========================================================================
-
- #include "CTCPServerThread.h"
- #include "CTCPResponder.h"
-
-
- // ---------------------------------------------------------------------------
- // • ~CTCPServerThread
- // ---------------------------------------------------------------------------
-
-
- CTCPServerThread::CTCPServerThread(
- UInt16 inMaxConnections,
- UInt16 inLocalPort,
- PP_PowerPlant::LTCPEndpoint* inNetworkEndpoint,
- CSimpleTCPServer* inServerMaster)
- : LThread( false,
- PP_PowerPlant::thread_DefaultStack,
- PP_PowerPlant::LThread::threadOption_Default,
- nil),
- mEndpoint(inNetworkEndpoint),
- mMaxConnections(inMaxConnections),
- mPort(inLocalPort),
- mServerMaster(inServerMaster)
- {
- mDisconnectReceived = mStartDisconnect = false;
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~CTCPServerThread
- // ---------------------------------------------------------------------------
-
- CTCPServerThread::~CTCPServerThread()
- {
- }
-
- // ---------------------------------------------------------------------------
- // • StartDisconnect
- // ---------------------------------------------------------------------------
-
- void
- CTCPServerThread::StartDisconnect()
- {
- if (!mStartDisconnect) {
- mStartDisconnect = true;
-
- if (mState == PP_PowerPlant::LThread::threadState_Waiting)
- ResumeServerThread();
- else
- mEndpoint->AbortThreadOperation(this);
- }
- }
-
- // ---------------------------------------------------------------------------
- // • SuspendThread
- // ---------------------------------------------------------------------------
-
- void
- CTCPServerThread::SuspendServerThread()
- {
- mConnectionSignal.Wait();
- }
-
- // ---------------------------------------------------------------------------
- // • ResumeThread
- // ---------------------------------------------------------------------------
-
- void
- CTCPServerThread::ResumeServerThread()
- {
- mConnectionSignal.Signal();
- }
-
- // ---------------------------------------------------------------------------
- // • Run
- // ---------------------------------------------------------------------------
-
- void*
- CTCPServerThread::Run()
- {
- try {
- PP_PowerPlant::LInternetAddress address(0, mPort);
- mEndpoint->Bind(address, mMaxConnections);
- mServerMaster->BindCompleted();
-
- // Endless loop: watch and wait for incomming connections.
- while (1) {
- try {
- SuspendServerThread(); //Nothing to do but wait for T_LISTEN
-
- if (mStartDisconnect)
- break;
-
- mEndpoint->Listen();
-
- if (mServerMaster->mConnectionCount < mMaxConnections) {
- CTCPResponder* theConnection = new CTCPResponder(mServerMaster->GetSuperCommander());
- theConnection->Accept(mServerMaster);
- } else {
- mEndpoint->RejectIncoming();
- mServerMaster->IncRejectionCount();
- }
-
- }
- catch(...) {
- }
- }
- }
- catch(...) {
- }
-
- // We're done with the connection… bail out.
-
- try {
- mEndpoint->Unbind();
- }
- catch(...) {
- }
-
- mServerMaster->ServerThreadDied();
- return nil;
- }
-